using System.Data;
using SuCasa.Infrastructure;
using SuCasa.Models;
using System;
using System.Linq;

namespace SuCasa.DataAccess
{
    public interface ICallLogRepository {
        bool AddCallLog(CallLog callLog);
        CallLogViewModel GetCallLogsByMember(Member member);
    }

    public class CallLogRepository : ICallLogRepository{
        readonly SuCasaDbContext _context;

        public CallLogRepository(SuCasaDbContext context){
            _context = context;
        }


        public bool AddCallLog(CallLog callLog){
            
            if (callLog.CallLogId == 0){
                _context.CallLogs.Add(callLog);
            }
            else
            {
                _context.CallLogs.Attach(callLog);
                _context.Entry(callLog).State = EntityState.Modified;
                
            }
            return _context.SaveChanges() > 0;
        }

        public CallLogViewModel GetCallLogsByMember(Member member)
        {
            String memberFullName = String.Format("{0} {1}", member.FirstName, member.LastName);
            return new CallLogViewModel
            {
                Name = memberFullName,
                ReceivedByMemberId = member.MemberId,
                CallLogs = _context.CallLogs.Where(x => x.ReceivedByMemberId == member.MemberId),
                CellPhone = member.CellPhone,
                PhoneNumber = member.HomePhone
            };
        }
    }
}